package com.tal.bcc.pad.cloud.utils;



import com.tal.bcc.pad.cloud.exception.CloudStorageException;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class WebUtils {
    public WebUtils() {
    }

    public static void main(String[] args) {
        Map<String, Object> array = new TreeMap();
        array.put("info", "");
        array.put("name", "123");
        System.out.println(httpBuildQuery(array));
    }

    public static String buildCanonicalRequestHeaderQuery(Map<String, String> headers) {
        Iterator<String> iterator = headers.keySet().iterator();
        StringBuilder builder = new StringBuilder();

        String key;
        while(iterator.hasNext()) {
            key = (String)iterator.next();
            if ("".contentEquals(builder)) {
                builder.append(key).append(':').append((String)headers.get(key));
            } else {
                builder.append("\n").append(key).append(':').append((String)headers.get(key));
            }
        }

        key = builder.toString();
        return key;
    }

    public static String httpBuildQuery(Map<String, Object> array) {
        String reString = "";
        if (array != null && array.size() != 0) {
            Iterator it = array.entrySet().iterator();

            while(it.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry)it.next();
                String key = (String)entry.getKey();
                Object value = entry.getValue();

                try {
                    reString = reString + URLEncoder.encode(key, "UTF-8") + "=" + URLEncoder.encode(value.toString(), "UTF-8") + "&";
                } catch (UnsupportedEncodingException var7) {
                    UnsupportedEncodingException e = var7;
                    throw new CloudStorageException("URL encode异常:" + e.getMessage());
                }
            }

            reString = reString.substring(0, reString.length() - 1);
            return reString;
        } else {
            return reString;
        }
    }

    public static byte[] inputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();

        int ch;
        while((ch = is.read()) != -1) {
            bytestream.write(ch);
        }

        byte[] data = bytestream.toByteArray();
        bytestream.close();
        return data;
    }

    public static String getStreamAsString(InputStream stream, String charset) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
            StringWriter writer = new StringWriter();

            int count;
            while((count = reader.read()) != -1) {
                writer.write(count);
            }

            String var5 = writer.toString();
            return var5;
        } finally {
            if (stream != null) {
                stream.close();
            }

        }
    }

    public static String getResponseCharset(String ctype) {
        String charset = "UTF-8";
        if (!"".equals(ctype)) {
            String[] params = ctype.split(";");
            String[] var3 = params;
            int var4 = params.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String param = var3[var5];
                param = param.trim();
                if (param.startsWith("charset")) {
                    String[] pair = param.split("=", 2);
                    if (pair.length == 2 && !"".equals(pair[1])) {
                        charset = pair[1].trim();
                    }
                    break;
                }
            }
        }

        return charset;
    }

    public static void getFileByBytes(byte[] bytes, String filePath) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;

        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }

            file = new File(filePath);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception var18) {
            Exception e = var18;
            e.printStackTrace();
        } finally {
            IOException e;
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException var17) {
                    e = var17;
                    e.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException var16) {
                    e = var16;
                    e.printStackTrace();
                }
            }

        }

    }
}
